package com.ruoyi.user.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.manage.domain.Activity;
import com.ruoyi.manage.domain.ActivityUser;
import com.ruoyi.manage.domain.User;
import com.ruoyi.manage.domain.vo.ActivityVo;
import com.ruoyi.manage.service.IActivityService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhangZhongGen
 */
@RestController
@RequestMapping("/userWeixin/activity")
@ApiOperation(value = "微信端-活动管理")
public class UserActivityController extends BaseController {

    @Autowired
    private IActivityService activityService;

    /**
     * 查询活动信息列表
     */
    @ApiOperation("查询活动信息列表")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        //如果学校id为0或null,表示查询全部活动,当需要先匹配==null的情况,
        // 因为如果是null,跟数字进行比较时,底层自动调用Long.parseLong(null) => 报错
        if(activity.getSchoolId()==null||activity.getSchoolId()==0)activity.setSchoolId(null);
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 获取活动信息详细信息
     */
    @ApiOperation("获取活动信息详细信息")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId) {
        ActivityUser build = ActivityUser.builder().userId(getUser().getId()).activityId(activityId).build();
        ActivityUser au = activityService.selectActivityByActivityUser(build);//活动-用户-关联数据
        Activity activity = activityService.selectActivityByActivityId(activityId);//活动数据
        ActivityVo activityVo = new ActivityVo();
        BeanUtils.copyProperties(activity,  activityVo);
        activityVo.setIsEnroll(
                au != null ? 1 : 0
        );
        return success(activityVo);
    }

    /**
     * 用户端-报名活动
     * @param activityId
     * @return
     */
    @ApiOperation("用户端-报名活动")
    @PostMapping("/enroll/{activityId}")
    public AjaxResult enroll(@PathVariable("activityId") Long activityId){
        ActivityUser activityUser = new ActivityUser(null, activityId, getUser().getId(), 0);
        return success(activityService.enroll(activityUser));
    }

    @ApiOperation("用户端-取消报名活动")
    @PutMapping("/cancelEnroll/{activityId}")
    public AjaxResult cancelEnroll(@PathVariable("activityId") Long activityId){
        ActivityUser au = ActivityUser.builder().activityId(activityId).userId(getUser().getId()).build();
        return success(activityService.cancelEnroll(au));
    }

    /**
     * 获取当前微信用户
     * @return
     */
    public User getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User principal = (User) authentication.getPrincipal();
        return principal;
    }
}
